package com.one.group.admin.service.impl;

import com.one.group.admin.mapper.DecorationInspectionMapper;
import com.one.group.service.DecorationInspectionService;
import com.one.group.model.entity.DecorationInspection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author kld
 * @description 装修验收服务实现类
 */
@Service
public class DecorationInspectionServiceImpl implements DecorationInspectionService {
    
    @Autowired
    private DecorationInspectionMapper decorationInspectionMapper;
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveDecorationInspection(DecorationInspection inspection) {
        // 参数验证
        if (inspection == null || inspection.getDecorationId() == null) {
            return false;
        }
        
        // 设置默认值
        inspection.setInspectionDate(new Date()); // 设置当前时间为验收日期
        inspection.setIsDeleted(0); // 未删除
        inspection.setCreateTime(new Date());
        inspection.setUpdateTime(new Date());
        
        // 检查是否已存在验收记录
        DecorationInspection existing = decorationInspectionMapper.findByDecorationId(inspection.getDecorationId());
        if (existing != null) {
            // 逻辑删除旧记录
            existing.setIsDeleted(1);
            existing.setUpdateTime(new Date());
            decorationInspectionMapper.update(existing);
        }
        
        // 保存新记录
        return decorationInspectionMapper.insert(inspection) > 0;
    }
    
    @Override
    public DecorationInspection findByDecorationId(Long decorationId) {
        if (decorationId == null) {
            return null;
        }
        return decorationInspectionMapper.findByDecorationId(decorationId);
    }
}